<?php
/**
 * 设计师服务亮点控制器
 */
namespace Beerus\Modules\Designer\Controllers;

use App\Http\Controllers\Controller;
use Biz\pockets\excel\DownExcel;
use Doraemon\model\DesignerAuth;
use Doraemon\model\Member;
use Doraemon\model\Designer\DesignerSpotFeedback;
use Encore\Admin\Facades\Admin;
use Illuminate\Http\Request;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use App\Pockets\GridDataModel as pGridDataModel;
use Encore\Admin\Widgets\Tab;

class FeedbackSpotController extends Controller
{

    const PAGE_SIZE = 10;

    public function __construct()
    {
        parent::__construct();
        Admin::script('$(".pjax-container").attr("id","pjax-container");');
    }

    // 对话列表
    public function index(Request $request, Content $content)
    {
        $page_size = $request->input("per_page", self::PAGE_SIZE);
        $page = $request->input("page", 1);

        $condition = [];
        if (!empty($request['fb_uid'])) {
            $condition['fb_uid'] = $request['fb_uid'];
        }
        if (!empty($request['fb_unick'])) {
            $fb_uid = Member::getUidByNick($request['fb_unick']);
            $condition['fb_uid'] = $fb_uid ? $fb_uid : 0;
        }
        $total = DesignerSpotFeedback::count($condition);
        $condition['ORDER'] = ['fb_id' => 'DESC'];
        $condition['LIMIT'] = [
            ($page - 1) * $page_size,
            $page_size
        ];
        $lists = DesignerSpotFeedback::select('*', $condition);

        $gridModel = new pGridDataModel($lists, $total, [
            'perPage' => $page_size,
        ]);

        $search_params = http_build_query($condition);
        $gridList = Admin::grid(
            $gridModel,
            function (Grid $grid) use ($gridModel, $search_params) {
                $grid->setActionClass(DropdownActions::class);
                $grid->paginate($gridModel->getPerPage());
                $grid->disableExport();
                $grid->disableRowSelector();
                $grid->disableCreateButton();
                // 添加导出按钮
                $grid->tools(function (Grid\Tools $tool) use ($grid, $search_params) {
                    $url = url('/admin/Designer/feedback_export?'. $search_params);
                    $button = '<div class="btn-group download" style="margin-right: 10px">
                        <a class="btn btn-sm btn-primary" target="_blank" href="'.$url.'">
                        <i class="fa fa-download"></i>
                        <span class="hidden-xs">导出反馈记录</span></a>
                    </div>';
                    $tool->append($button);
                });

                $grid->filter(function (Grid\Filter $filter) {
                    $filter->disableIdFilter();
                    $filter->column(1 / 2, function ($filter) {
                        $filter->equal('fb_uid', '反馈人UID')->placeholder('请输入反馈人UID');
                    });
                    $filter->column(1 / 2, function ($filter) {
                        $filter->equal('fb_unick', '反馈人昵称')->placeholder('请输入反馈人昵称，不支持模糊匹配');
                    });
                    $filter->expand();
                });

                $grid->column('fb_id', '反馈ID');
                $grid->column('fb_spot_name', '亮点名称');
                $grid->column('fb_feedback_uid', '反馈人UID')->display(function() {
                    return $this->fb_uid;
                });
                $grid->column('fb_unick', '反馈人')->display(function() {
                    return Member::get('nick', ['uid' => $this->fb_uid]);
                });
                $grid->column('add_time', '创建时间')->display(function() {
                    return date('Y-m-d H:i:s', $this->add_time);
                });
                $grid->disableActions();
            }
        );

        $tab = new Tab();
        $tab->add('反馈服务亮点列表', $gridList->render());
        $content->row($tab);
        return $content
            ->header('反馈服务亮点列表')
            ->description(' ')
            ->breadcrumb(['text' => '反馈服务亮点列表']);
    }

    // 导出反馈
    public function exportFeedback(Request $request) {
        $request = $request->all();
        $condition = [];
        if (!empty($request['fb_uid'])) {
            $condition['fb_uid'] = $request['fb_uid'];
        }
        if (!empty($request['fb_unick'])) {
            $fb_uid = Member::getUidByNick($request['fb_unick']);
            $condition['fb_uid'] = $fb_uid ? $fb_uid : 0;
        }
        $lists = DesignerSpotFeedback::select('*', $condition);
        $lists = is_array($lists) ? $lists : [];
        $data = [];
        if (!empty($lists)) {
            $user_ids = array_column($lists, 'fb_uid');
            $member_info = Member::getUsers($user_ids);
            foreach ($lists as $item) {
                $data[] = [
                    'uid' => $item['fb_uid'],
                    'nick' => empty($member_info[$item['fb_uid']]['nick']) ? '' : $member_info[$item['fb_uid']]['nick'],
                    'spot_name' => $item['fb_spot_name'],
                    'add_time' => date('Y-m-d H:i:s', $item['add_time'])
                ];
            }
        }
        // 设置列宽
        $header = [
            '用户UID', '用户昵称', '亮点名称', '反馈时间'
        ];
        $letter = range('A', 'D');
        $excel_obj = new DownExcel();
        $excel_obj->setFileName('设计师反馈亮点 - ' . date('YmdHis'));
        $excel_obj->setSheetTitle('设计师反馈亮点');
        // 设置列宽
        $widths = [
            'A' => 12,
            'B' => 22,
            'C' => 18,
            'D' => 18,
        ];
        $excel_obj->setWidth($widths);
        $excel_obj->setDefaultStyleAlignment();
        foreach ($header as $key => $value) {
            // 设置字体
            $excel_obj->setFrontName($letter[$key]);
            // 设置加粗
            $excel_obj->setFrontBold("$letter[$key]1");
            // 设置大小
            $excel_obj->setFrontSize($letter[$key]);
        }
        $excel_obj->create($data, $header);
    }
}
